#!/bin/sh
# shellcheck shell=dash disable=SC2169 source=/dev/null
#
# startup script to start/stop third-party addons
#

start() {
  [[ -r /var/hm_mode ]] && . /var/hm_mode

  # skip this startup if not in normal mode
  [[ "${HM_MODE}" != "NORMAL" ]] && exit 0

  echo -n "Initializing Third-Party Addons: "

  if [[ ! -e /etc/config/safemode ]]; then

    # adjust the oom score (which is inherited by run-parts)
    # to ensure that others are killed first in case of low memory situations
    echo 100 >/proc/$$/oom_score_adj

    # call /usr/local/etc/rc.prelocal if it exists and is
    # executable
    if [[ -x /usr/local/etc/rc.prelocal ]]; then
      echo -n "rc.prelocal, "
      /usr/bin/timeout 120 /usr/local/etc/rc.prelocal
    fi

    # start init of third-party addons one after another
    /bin/run-parts -a init /etc/config/rc.d >/dev/null 2>&1

    echo "OK"
  else
    echo "skipping (safemode)"
  fi
}

case "$1" in
  start)
    start
  ;;
  stop)
    # nothing
  ;;
  *)
    echo "Usage: $0 {start|stop}"
    exit 1
esac

exit 0
